package com.situ.mall.service.impl;


import com.situ.imall.util.JSONResult;
import com.situ.imall.util.LayUiTableDataUtil;
import com.situ.imall.util.MD5Util;
import com.situ.mall.mapper.UserMapper;

import com.situ.mall.pojo.User;
import com.situ.mall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Service
@Transactional
//    Propagation propagation() default Propagation.REQUIRED;
public class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;

    /**
     * 查询所有用户
     **/
    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    /**
     * 用户登录验证
     **/
    @Override
    public JSONResult login(Map params, HttpSession session) {
        String username = (String) params.get("username");
        String password = (String) params.get("password");
        //验证码
        //前端传来的验证码
        String code = (String) params.get("code");
        String codeInSession = (String) session.getAttribute("validateCode");
        //进行对比
        if (!codeInSession.equalsIgnoreCase(code)) {

            return JSONResult.error("验证码错误");
        }
        //前后端比较密码
        String finalPwd = MD5Util.MD5Encode(password);
        params.put("password", finalPwd);
        User user = userMapper.getUserByUsernameAndPassword(params);
        if (user == null) {
            return JSONResult.error("用户名或密码错误");

        } else {
            //保存到session域做登录验证
            session.setAttribute("user", user);
            return JSONResult.ok("登录成功!");
        }

    }

    /**
     * 用户注册
     **/
    @Override
    public JSONResult register(Map params, HttpSession session) {

        if (userMapper.queryUserByName(params) != null) {
            //重名
            return JSONResult.error("用户名已被注册！");
        }
        //正常注册
        else {
            //密码加密后保存数据库
            String password = (String) params.get("password");
            String finalPass = MD5Util.MD5Encode(password);
            params.put("password", finalPass);
            userMapper.addUser(params);
            String username = (String) params.get("username");
            //保存用户名，注册成功后回显、、
            session.setAttribute("registerUser", username);
            return JSONResult.ok("注册成功!");
        }
    }
    @Override
    public List queryAllUsers() {
        return userMapper.queryAllUsers();
    }

    /**
     * 分页操作
     */
    @Override
    public LayUiTableDataUtil selectByPage(Integer page, Integer limit) {
        //处理分页数据
        Integer offset = (page - 1) * limit;
        List<User> list = userMapper.selectPage(offset, limit);
        Integer totalCount = userMapper.selectTotalCount();
        return LayUiTableDataUtil.ok(totalCount, list);
    }

    /**
     * 后台添加新用户
     */
    @Override
    public JSONResult addUser(Map params) {
        //添加新用户的时候用MD5 加密
        String password = (String) params.get("password");
        String finalPass = MD5Util.MD5Encode(password);
        params.put("password", finalPass);
        userMapper.addUser(params);
        return JSONResult.ok("添加成功");

    }

    @Override
    public JSONResult updateUserById(Map params) {
        userMapper.updateUserById(params);
        return JSONResult.ok("修改成功!");
    }

    @Override
    public LayUiTableDataUtil deleteUser(int id) {
        int i = userMapper.deleteUser(id);
        return LayUiTableDataUtil.ok("删除成功", i);
    }

    /**
     * 根据id查询
     */
    @Override
    public User queryUserById(int id) {
        return userMapper.queryUserById(id);
    }


    /**
     * 批量删除
     */
    @Override
    public JSONResult deleteAll(String[] array) {
        userMapper.deleteAll(array);
        return JSONResult.ok("删除成功");
    }
    @Override
    public List<Map<String, Object>> getEchartsData() {
        return userMapper.getEchartsData();
    }
}
